package com.itheima.controller;


import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.naming.TimeLimitExceededException;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    JedisPool jedisPool;

    @RequestMapping("/send4Order")
    public Result send4Order(String telephone){
        //1 生成验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        //2、调用短信工具发送短信
        SMSUtils.sendSmsCode(telephone,String.valueOf(code));
//        System.out.println("code:" + code);
        //3、把验证码存入redis(要加入redis 配置文件,要让spring知道有这个文件)

        //如果用手机号码作为key如果其他业务场景发的短信会把当前验证码覆盖,所以我们可以给手机号码加一个前缀，或者后缀
        jedisPool.getResource().setex(telephone + RedisMessageConstant.SENDTYPE_ORDER,60 * 60 * 24,String.valueOf(code));

        return Result.success("");
    }



    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        //生成验证码
        Integer code = ValidateCodeUtils.generateValidateCode(6);
         //调用短信工具发送短信
        SMSUtils.sendSmsCode(telephone,String.valueOf(code));
        //把验证码存入redis 在配置文件中扫描 让spring知道该文件
        //防止短信错乱加入标记
        jedisPool.getResource().setex(telephone + RedisMessageConstant.SENDTYPE_LOGIN,60 * 60 * 24,String.valueOf(code));
        return Result.success("");
    }

}
